﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using ECP.Model;
using ECP.Service;
using ECP.Service.Utilities;
using ECP.Utilities;
using ECP.Utilities.Controls;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;

namespace ECP.Client.Module
{
    public partial class ModuleAdminFrm : FormBase, IModelInfo
    {
        private string currentEntityID = string.Empty;
        private DataTable DTModule = new DataTable(BaseModuleTable.TableName);
        private DataTable DTModuleList = new DataTable(BaseModuleTable.TableName);
        private Control LastControl;
        private string moduleList = string.Empty;
        private string parentEntityID = string.Empty;
        private bool PermissionAccess;
        private bool PermissionAdd;
        private bool PermissionAddRoot;
        private bool PermissionDelete;
        private bool PermissionEdit;
        private bool PermissionExport;
        private bool PermissionModulePermission;
        private bool PermissionMove;

        private ModuleSelectFrm ModuleSelectFrm;
        public event FormBase.SetControlStateEventHandler OnButtonStateChange;

        public ModuleAdminFrm()
        {
            InitializeComponent();
            DisplayType = DisplayTypes.TabPage;
        }

        public string ModelCode
        {
            get { return this.GetType().FullName; }
        }

        public string ModelName
        {
            get { return this.Text; }
        }

        public void StartModel()
        {
            base.StartModel();
        }

        private void GetPermission()
        {
            this.PermissionAccess = base.IsAuthorized("ModuleAdmin.Access");
            this.PermissionAddRoot = base.IsAuthorized("ModuleAdmin.AddRoot");
            this.PermissionAdd = base.IsAuthorized("ModuleAdmin.Add");
            this.PermissionEdit = base.IsAuthorized("ModuleAdmin.Edit");
            this.PermissionDelete = base.IsAuthorized("ModuleAdmin.Delete");
            this.PermissionMove = base.IsAuthorized("ModuleAdmin.Move");
            this.PermissionExport = base.IsAuthorized("ModuleAdmin.Export");
            this.PermissionModulePermission = base.IsAuthorized("ModuleAdmin.ModulePermission");
        }
        public IList<PermissionInfo> Permissions
        {
            get
            {
                return new List<PermissionInfo>()
                {
                    new PermissionInfo("ModuleAdmin.Access", "模块管理") ,
                    new PermissionInfo("ModuleAdmin.AddRoot", "添加根模块") ,
                    new PermissionInfo("ModuleAdmin.Add", "模块添加"),
                    new PermissionInfo("ModuleAdmin.Edit","模块编辑"),
                    new PermissionInfo("ModuleAdmin.Delete","删除模块"),
                    new PermissionInfo("ModuleAdmin.Move","移动模块"),
                    new PermissionInfo("ModuleAdmin.Export","导出模块"),
                    new PermissionInfo("ModuleAdmin.ModulePermission","管理权限")
                };
            }
        }

        public void Edit()
        {
            if (this.LastControl == this.grdModule)
            {
                this.EditGrid();
            }
            if (this.LastControl == this.tvModule)
            {
                this.EditTree();
            }
        }

        private void EditGrid()
        {
            if (BaseInterfaceLogic.CheckInputSelect(this.DTModuleList, BaseBusinessLogic.SelectedColumn, false))
            {
                ModuleEditFrm edit = new ModuleEditFrm(BaseInterfaceLogic.GetDataGridViewEntityID(this.grdModuleView, BaseModuleTable.FieldID));
                if (edit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    base.FormLoaded = false;
                    this.FormOnLoad();
                    base.FormLoaded = true;
                }
            }
        }

        private void EditTree()
        {
            if (this.tvModule.FocusedNode != null)
            {
                ModuleEditFrm edit = new ModuleEditFrm(this.tvModule.FocusedNode.GetValue(colParentID).ToString());
                if (edit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    base.FormLoaded = false;
                    this.FormOnLoad();
                    base.FormLoaded = true;
                }
            }
        }

        private void FormOnLoad()
        {
            this.Cursor = Cursors.WaitCursor;
            base.Localization(this);
            try
            {
                this.DTModule = ServiceManager.Instance.ModuleService.GetDT(base.UserInfo);
                this.GetPermission();
                this.BindData(true);


            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void GetModuleList(bool reloadTree)
        {
            if (reloadTree || base.FormLoaded)
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    this.DTModuleList = ServiceManager.Instance.ModuleService.GetDTByParent(base.UserInfo, this.ParentEntityID);
                    this.DataTableAddColumn();
                    this.DTModuleList.DefaultView.Sort = BaseModuleTable.FieldSortCode;
                    this.grdModule.DataSource = this.DTModuleList.DefaultView;
                    this.ucTableSort.DataBind(base.UserInfo, this.grdModuleView, this.PermissionEdit);
                    this.SetControlState();
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }



        private string[] GetSelecteIDs()
        {
            return BaseInterfaceLogic.GetSelecteIDs(this.DTModuleList, BaseModuleTable.FieldID, BaseBusinessLogic.SelectedColumn);
        }

        private string[] GetTreeSort()
        {
            string[] strArray = new string[0];
            if (base.UserInfo.IsAdministrator)
            {
                this.moduleList = string.Empty;
                for (int i = 0; i < this.tvModule.Nodes.Count; i++)
                {
                    this.GetTreeSort(this.tvModule.Nodes[i]);
                }
                if (this.moduleList.Length > 0)
                {
                    this.moduleList = this.moduleList.Substring(0, this.moduleList.Length - 1);
                    strArray = this.moduleList.Split(new char[] { ',' });
                }
            }
            return strArray;
        }

        private void GetTreeSort(TreeListNode TreeNode)
        {
            this.moduleList = this.moduleList + TreeNode.GetValue(this.colParentID).ToString() + ",";
            for (int i = 0; i < TreeNode.Nodes.Count; i++)
            {
                this.GetTreeSort(TreeNode.Nodes[i]);
            }
        }

        private void LoadTree()
        {
            this.tvModule.BeginUpdate();
            this.tvModule.Nodes.Clear();
            this.LoadTreeModule();
            this.tvModule.EndUpdate();
            this.tvModule.CollapseAll();
            if (this.tvModule.Nodes.Count > 0)
            {
                this.tvModule.Nodes[0].Expanded = true;
            }

        }

        private void LoadTreeModule()
        {
            if (BaseSystemInfo.UseScopeAdmin && !base.UserInfo.IsAdministrator)
            {
                BaseInterfaceLogic.CheckTreeParentID(this.DTModule, BaseModuleTable.FieldID, BaseModuleTable.FieldParentID);
            }
            BaseInterfaceLogic.LoadTreeNodes(this.DTModule, BaseModuleTable.FieldID, BaseModuleTable.FieldParentID, BaseModuleTable.FieldFullName, this.tvModule);
        }


        private void Pothook()
        {
            if ((this.LastControl == this.grdModule) && BaseInterfaceLogic.CheckInputSelect(this.DTModuleList, BaseBusinessLogic.SelectedColumn, true))
            {
                string moduleFullName = BaseBusinessLogic.GetProperty(this.DTModuleList, this.CurrentEntityID, BaseModuleTable.FieldFullName);
                if (this.EntityID.Length > 0)
                {
                    new ModulePermissionAdminFrm(this.CurrentEntityID, moduleFullName).ShowDialog(this);
                }
            }
            if (this.LastControl == this.tvModule)
            {
                string str2 = BaseBusinessLogic.GetProperty(this.DTModule, this.ParentEntityID, BaseModuleTable.FieldFullName);
                if (this.ParentEntityID.Length > 0)
                {
                    new ModulePermissionAdminFrm(this.ParentEntityID, str2).ShowDialog(this);
                }
            }
        }

        public void Save()
        {
            if (this.CheckInputBatchSave())
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    this.BatchSave();
                }
                catch (Exception exception)
                {
                    base.ProcessException(exception);
                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
            }
        }

        public int SetBottom()
        {
            return this.ucTableSort.SetBottom();
        }

        public void SetControlState()
        {
            this.SetSortButton(false);
            this.mItmAdd.Enabled = false;
            this.mItmEdit.Enabled = false;

            this.mItmDelete.Enabled = false;
            this.mItmModulePermission.Enabled = false;
            this.mItmSetSortCode.Enabled = false;
            this.btnBatchDelete.Enabled = false;
            this.btnEdit.Enabled = false;
            this.btnBatchSave.Enabled = false;

            this.btnModulePermission.Enabled = false;
            this.btnAdd.Enabled = this.PermissionAdd;
            if ((this.DTModuleList.DefaultView.Count >= 1) || (this.tvModule.Nodes.Count > 0))
            {
                this.btnEdit.Enabled = this.PermissionEdit;

                this.btnModulePermission.Enabled = this.PermissionEdit;
                this.btnBatchDelete.Enabled = this.PermissionDelete;
            }
            if (this.DTModuleList.DefaultView.Count >= 1)
            {
                this.btnBatchSave.Enabled = this.PermissionEdit;
            }
            if (this.DTModuleList.DefaultView.Count > 1)
            {
                this.SetSortButton(this.PermissionEdit);
            }
            if ((this.DTModule.DefaultView.Count > 0) && (this.tvModule.Nodes.Count > 0))
            {
                this.mItmAdd.Enabled = this.PermissionAdd;
                this.mItmEdit.Enabled = this.PermissionEdit;
                this.mItmDelete.Enabled = this.PermissionDelete;
                this.mItmModulePermission.Enabled = this.PermissionModulePermission;
                this.mItmSetSortCode.Enabled = this.PermissionMove;
            }
            this.mItmAddRoot.Enabled = this.PermissionAddRoot;
            if (this.OnButtonStateChange != null)
            {
                bool setTopEnabled = this.ucTableSort.SetTopEnabled;
                bool setUpEnabled = this.ucTableSort.SetUpEnabled;
                bool setDownEnabled = this.ucTableSort.SetDownEnabled;
                bool setBottomEnabled = this.ucTableSort.SetBottomEnabled;
                bool enabled = this.btnAdd.Enabled;
                bool edit = this.btnEdit.Enabled;
                bool batchDelete = this.btnBatchDelete.Enabled;
                bool batchSave = this.btnBatchSave.Enabled;
                this.OnButtonStateChange(setTopEnabled, setUpEnabled, setDownEnabled, setBottomEnabled, enabled, edit, batchDelete, batchSave);
            }
        }

        public int SetDown()
        {
            return this.ucTableSort.SetDown();
        }

        public void SetSortButton(bool enabled)
        {
            this.ucTableSort.SetSortButton(enabled);
        }

        public int SetTop()
        {
            return this.ucTableSort.SetTop();
        }

        public int SetUp()
        {
            return this.ucTableSort.SetUp();
        }

        private int SingleDelete()
        {
            int num = 0;
            if (this.tvModule.FocusedNode != null)
            {
                this.Cursor = Cursors.WaitCursor;
                if (!CheckDeleteByID(ParentEntityID))
                {
                    this.Cursor = Cursors.Default;
                    return 0;
                }
                if (!BaseInterfaceLogic.NodeAllowDelete(this.tvModule.FocusedNode))
                {
                    XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0035, new string[] { this.tvModule.FocusedNode.GetDisplayText(this.tvModule.FocusedNode.Id) }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else if (XtraMessageBox.Show(AppMessage.MSG0015, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK)
                {
                    base.FormLoaded = false;
                    num = ServiceManager.Instance.ModuleService.Delete(base.UserInfo, this.parentEntityID);
                    this.tvModule.Nodes.Remove(this.tvModule.FocusedNode);
                    this.ParentEntityID = string.Empty;
                    base.FormLoaded = true;
                }
                this.Cursor = Cursors.Default;
            }
            return num;
        }



        public string CurrentEntityID
        {
            get
            {
                if (this.LastControl == this.tvModule)
                {
                    this.currentEntityID = this.parentEntityID;
                }
                else
                {
                    this.currentEntityID = this.EntityID;
                }
                return this.currentEntityID;
            }
            set
            {
                this.currentEntityID = value;
            }
        }

        public string EntityID
        {
            get
            {
                return BaseInterfaceLogic.GetDataGridViewEntityID(this.grdModuleView, BaseModuleTable.FieldID);
            }
        }

        public string ParentEntityID
        {
            get
            {
                if ((this.tvModule.FocusedNode != null))
                {
                    this.parentEntityID = this.tvModule.FocusedNode.GetValue(colParentID).ToString();
                }
                else
                {
                    this.parentEntityID = string.Empty;
                }
                return this.parentEntityID;
            }
            set
            {
                this.parentEntityID = value;
            }
        }

        public string Add()
        {
            return this.Add(false);
        }

        public string Add(bool root)
        {
            ModuleAddFrm add;
            string entityID = string.Empty;
            if ((root || (this.ParentEntityID.Length == 0)) || (this.tvModule.FocusedNode == null))
            {
                add = new ModuleAddFrm();

            }
            else
            {
                add = new ModuleAddFrm(this.parentEntityID, this.tvModule.FocusedNode.GetValue(colTreelist).ToString());
            }
            if (add.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                entityID = add.EntityID;
                this.parentEntityID = add.SelectID;
                base.FormLoaded = false;
                this.FormOnLoad();
                base.FormLoaded = true;
            }
            return entityID;
        }

        private int BatchDelete()
        {
            int num = 0;
            if (this.CheckInputBatchDelete() && (XtraMessageBox.Show(AppMessage.MSG0015, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.OK))
            {
                base.FormLoaded = false;
                num = ServiceManager.Instance.ModuleService.BatchDelete(base.UserInfo, this.GetSelecteIDs());
                this.DTModule = ServiceManager.Instance.ModuleService.GetDT(base.UserInfo);
                this.BindData(true);
                base.FormLoaded = true;
            }
            return num;
        }


        private void BatchSave()
        {
            ServiceManager.Instance.ModuleService.BatchSave(base.UserInfo, this.DTModuleList);
            this.GetModuleList(true);
            if (BaseSystemInfo.ShowInformation)
            {
                XtraMessageBox.Show(AppMessage.MSG0012, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void BindData(bool reloadTree)
        {
            if (reloadTree)
            {
                this.LoadTree();

                if ((this.tvModule.FocusedNode == null || this.tvModule.FocusedNode.GetValue(colParentID).ToString() != this.parentEntityID) && (this.tvModule.Nodes.Count > 0))
                {
                    if (this.parentEntityID.Length == 0)
                    {
                        this.tvModule.FocusedNode = this.tvModule.Nodes[0];
                    }
                    else
                    {
                        BaseInterfaceLogic.FindTreeNode(this.tvModule, this.parentEntityID, colParentID);
                        if (BaseInterfaceLogic.TargetNode != null)
                        {
                            this.tvModule.FocusedNode = BaseInterfaceLogic.TargetNode;
                            BaseInterfaceLogic.ExpandTreeNode(this.tvModule);
                        }
                    }
                    if (this.tvModule.FocusedNode != null)
                    {
                        this.tvModule.FocusedNode.Expanded = true; ;
                    }
                }
                this.GetModuleList(reloadTree);
            }

            if (!this.PermissionEdit)
            {
                this.colCheckBox.OptionsColumn.AllowEdit = !this.PermissionEdit;
                this.colMESSAGE.OptionsColumn.AllowEdit = !this.PermissionEdit;
                this.colNote.OptionsColumn.AllowEdit = !this.PermissionEdit;
                this.colMESSAGE.AppearanceCell.BackColor = Color.White;
                this.colNote.AppearanceCell.BackColor = Color.White;
            }
        }
        private bool CheckDeleteByID(string modulID)
        {
            BaseModuleEntity entity = new BaseModuleEntity();
            bool flag = false;
            foreach (DataRow row in this.DTModule.Rows)
            {
                if (row.RowState != DataRowState.Deleted && row[BaseMessageTable.FieldID].ToString() == modulID)
                {
                    entity.GetFrom(row);
                    if (!entity.AllowDelete)
                    {
                        flag = false;
                        XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0018, new string[] { entity.FullName }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return flag;
                    }
                }
            }
            return true;
        }
        private bool CheckInputBatchDelete()
        {
            bool flag = false;
            BaseModuleEntity entity = new BaseModuleEntity();
            foreach (DataRow row in this.DTModuleList.Rows)
            {
                if (row.RowState != DataRowState.Deleted)
                {
                    bool flag3 = true;
                    if (row[BaseBusinessLogic.SelectedColumn].ToString() == flag3.ToString())
                    {
                        entity.GetFrom(row);
                        if (!entity.AllowDelete)
                        {
                            flag = false;
                            XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0018, new string[] { entity.FullName }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            return flag;
                        }
                        flag = true;
                        string id = row[BaseModuleTable.FieldID].ToString();
                        BaseInterfaceLogic.FindTreeNode(this.tvModule, id, colParentID);
                        if ((BaseInterfaceLogic.TargetNode != null) && !BaseInterfaceLogic.NodeAllowDelete(BaseInterfaceLogic.TargetNode))
                        {
                            XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0035, new string[] { BaseInterfaceLogic.TargetNode.GetDisplayText(BaseInterfaceLogic.TargetNode.Id) }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            flag = false;
                        }
                        return flag;
                    }
                }
            }
            if (!flag)
            {
                XtraMessageBox.Show(AppMessage.MSG0023, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return flag;
        }

        private bool CheckInputBatchSave()
        {
            int num = 0;
            bool flag = false;
            BaseModuleEntity entity = new BaseModuleEntity();
            foreach (DataRow row in this.DTModuleList.Rows)
            {
                if (row.RowState == DataRowState.Modified)
                {
                    entity.GetFrom(row);
                    if (!entity.AllowEdit && ((row[BaseModuleTable.FieldFullName, DataRowVersion.Original] != row[BaseModuleTable.FieldFullName, DataRowVersion.Current]) || (row[BaseModuleTable.FieldDescription, DataRowVersion.Original] != row[BaseModuleTable.FieldDescription, DataRowVersion.Current])))
                    {
                        flag = false;
                        XtraMessageBox.Show(AppMessage.Format(AppMessage.MSG0020, new string[] { entity.FullName }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return flag;
                    }
                    num++;
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    num++;
                }
            }
            flag = num > 0;
            if (!flag)
            {
                XtraMessageBox.Show(AppMessage.MSG0004, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return flag;
        }
        private bool CheckInputMove()
        {
            bool flag = true;
            if (this.LastControl == this.tvModule)
            {
                BaseInterfaceLogic.FindTreeNode(this.tvModule, this.parentEntityID, colParentID);
                TreeListNode targetNode = BaseInterfaceLogic.TargetNode;
                BaseInterfaceLogic.FindTreeNode(this.tvModule, this.ModuleSelectFrm.SelectedID, colParentID);
                TreeListNode targetTreeNode = BaseInterfaceLogic.TargetNode;
                if (!BaseInterfaceLogic.TreeNodeCanMoveTo(targetNode, targetTreeNode, colTreelist))
                {
                    MessageBox.Show(AppMessage.Format(AppMessage.MSG0036, new string[] { targetNode.GetValue(colTreelist).ToString(), targetTreeNode.GetValue(colTreelist).ToString() }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    flag = false;
                }
            }
            if (this.LastControl == this.grdModule)
            {
                BaseInterfaceLogic.FindTreeNode(this.tvModule, this.ModuleSelectFrm.SelectedID, colParentID);
                TreeListNode node3 = BaseInterfaceLogic.TargetNode;
                string[] selecteIDs = this.GetSelecteIDs();
                for (int i = 0; i < selecteIDs.Length; i++)
                {
                    BaseInterfaceLogic.FindTreeNode(this.tvModule, selecteIDs[i], colParentID);
                    TreeListNode currentTreeNode = BaseInterfaceLogic.TargetNode;
                    if (!BaseInterfaceLogic.TreeNodeCanMoveTo(currentTreeNode, node3, colTreelist))
                    {
                        MessageBox.Show(AppMessage.Format(AppMessage.MSG0036, new string[] { currentTreeNode.GetValue(colTreelist).ToString(), node3.GetValue(colTreelist).ToString() }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        return false;
                    }
                }
            }
            return flag;
        }

        private void SingleMove()
        {
            this.ModuleSelectFrm = new ModuleSelectFrm(this.parentEntityID);
            this.ModuleSelectFrm.AllowNull = true;
            this.ModuleSelectFrm.OnButtonConfirmClick += new ModuleSelectFrm.ButtonConfirmEventHandler(this.CheckInputMove);
            if (this.ModuleSelectFrm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                base.FormLoaded = false;
                ServiceManager.Instance.ModuleService.MoveTo(base.UserInfo, this.CurrentEntityID, this.ModuleSelectFrm.SelectedID);
                this.FormOnLoad();
                base.FormLoaded = true;
                this.Cursor = Cursors.Default;
            }
        }
        private void BatchMove()
        {
            this.ModuleSelectFrm = new ModuleSelectFrm(this.parentEntityID);
            this.ModuleSelectFrm.AllowNull = true;
            this.ModuleSelectFrm.OnButtonConfirmClick += new ModuleSelectFrm.ButtonConfirmEventHandler(this.CheckInputMove);
            if (this.ModuleSelectFrm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                base.FormLoaded = false;
                this.ParentEntityID = this.ModuleSelectFrm.SelectedID;
                ServiceManager.Instance.ModuleService.BatchMoveTo(base.UserInfo, this.GetSelecteIDs(), this.ModuleSelectFrm.SelectedID);
                this.FormOnLoad();
                base.FormLoaded = true;
                this.Cursor = Cursors.Default;
            }
        }
        private void DataTableAddColumn()
        {
            BaseInterfaceLogic.DataTableAddColumn(this.DTModuleList, BaseBusinessLogic.SelectedColumn);
        }
        private void MoveObject()
        {
            if ((this.LastControl == this.grdModule) && BaseInterfaceLogic.CheckInputSelectAnyOne(this.DTModuleList, BaseBusinessLogic.SelectedColumn))
            {
                this.BatchMove();
            }
            if ((this.LastControl == this.tvModule) && (this.ParentEntityID.Length > 0))
            {
                this.SingleMove();
            }
        }
        public int Delete()
        {
            int num = 0;
            if ((this.LastControl == this.grdModule) && BaseInterfaceLogic.CheckInputSelectAnyOne(this.DTModuleList, BaseBusinessLogic.SelectedColumn))
            {
                num = this.BatchDelete();
            }
            if (this.LastControl == this.tvModule)
            {
                num = this.SingleDelete();
            }
            return num;
        }

        private void ModuleAdminFrm_Load(object sender, EventArgs e)
        {
            base.FormLoaded = false;
            this.FormOnLoad();

            this.LastControl = this.tvModule;
            base.FormLoaded = true;
        }

        private string ucTableSort_GetDegressionEvent(ECP.Utilities.BaseUserInfo userInfo, string tableName)
        {
            return SequenceService.Instance.GetDegression(userInfo, tableName);
        }

        private string ucTableSort_GetSequenceEvent(ECP.Utilities.BaseUserInfo userInfo, string tableName)
        {
            return SequenceService.Instance.GetSequence(userInfo, tableName);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.Add(false);
        }

        private void btnBatchDelete_Click(object sender, EventArgs e)
        {
            this.Delete();
        }

        private void btnBatchSave_Click(object sender, EventArgs e)
        {
            this.Save();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }



        private void btnPothook_Click(object sender, EventArgs e)
        {
            this.Pothook();
        }

        private void FrmModuleAdmin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (BaseInterfaceLogic.IsModfiedAnyOne(this.DTModuleList))
            {
                switch (XtraMessageBox.Show(AppMessage.MSG0045, AppMessage.MSG0000, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3))
                {
                    case System.Windows.Forms.DialogResult.Cancel:
                        e.Cancel = true;
                        return;

                    case System.Windows.Forms.DialogResult.Yes:
                        this.BatchSave();
                        break;
                }
            }
        }

        private void FrmModuleAdmin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 0x74)
            {
                base.FormLoaded = false;
                this.FormOnLoad();
                base.FormLoaded = true;
            }
        }

        private void FrmModuleAdmin_Load(object sender, EventArgs e)
        {
            base.FormLoaded = false;
            this.FormOnLoad();
            base.FormLoaded = true;
        }


        private void grdModule_DoubleClick(object sender, EventArgs e)
        {
            this.btnEdit.PerformClick();
        }
        private void grdModule_Click(object sender, EventArgs e)
        {
            this.LastControl = this.grdModule;
        }

        private void mItmAdd_Click(object sender, EventArgs e)
        {
            this.btnAdd.PerformClick();
        }

        private void mItmAddRoot_Click(object sender, EventArgs e)
        {
            this.Add(true);
        }

        private void mItmDelete_Click(object sender, EventArgs e)
        {
            this.SingleDelete();
        }

        private void mItmEdit_Click(object sender, EventArgs e)
        {
            ModuleEditFrm edit;
            if (this.tvModule.FocusedNode == null)
            {
                edit = new ModuleEditFrm();
            }
            else
            {
                edit = new ModuleEditFrm(this.tvModule.FocusedNode.GetValue(colParentID).ToString());
            }
            if (edit.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                base.FormLoaded = false;
                this.FormOnLoad();
                base.FormLoaded = true;
            }
        }

        private void mItmModulePermission_Click(object sender, EventArgs e)
        {
            this.btnModulePermission.PerformClick();
        }



        private void mItmSetSortCode_Click(object sender, EventArgs e)
        {
            ServiceManager.Instance.ModuleService.SetSortCode(base.UserInfo, this.GetTreeSort());
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
        }

        private void tvModule_Click(object sender, EventArgs e)
        {
            this.LastControl = this.tvModule;
            if (this.tvModule.FocusedNode == null)
            {
                this.tvModule.ContextMenuStrip = null;
            }
            else
            {
                this.tvModule.ContextMenuStrip = this.cMnuModule;
            }
        }

        private void tvModule_NodeChanged(object sender, DevExpress.XtraTreeList.NodeChangedEventArgs e)
        {
            if (e.Node.HasChildren)
            {
                e.Node.ImageIndex = 1;
                e.Node.SelectImageIndex = 1;
            }
            else
            {
                e.Node.SelectImageIndex = 2;
            }

        }

        private void tvModule_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            if (base.FormLoaded && (this.ParentEntityID.Length > 0))
            {
                this.GetModuleList(true);
            }
        }

        private void btnModulePermission_Click(object sender, EventArgs e)
        {
            this.Pothook();
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
            this.MoveObject();
        }

        private void 移动MToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.SingleMove();
        }


        /// <summary>
        /// 拖拽的节点
        /// </summary>
        private TreeListNode dragNode;
        /// <summary>
        /// 原始父节点
        /// </summary>
        private TreeListNode orgParentNode;
        /// <summary>
        /// 当前父节点
        /// </summary>
        private TreeListNode pNode = null;
        private int index;
        /// <summary>
        /// 拖拽完成时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void emTreeList_DragDrop(object sender, DragEventArgs e)
        {

            TreeListHitInfo hitinfo = tvModule.CalcHitInfo(tvModule.PointToClient(new Point(e.X, e.Y)));
            TreeListNode node = hitinfo.Node;//当前点击的节点

            if (BaseInterfaceLogic.TreeNodeCanMoveTo(dragNode, node, colTreelist) && (!BaseSystemInfo.ShowInformation || (MessageBox.Show(AppMessage.Format(AppMessage.MSG0038, new string[] { this.dragNode.GetValue(colTreelist).ToString(), node.GetValue(colTreelist).ToString() }), AppMessage.MSG0000, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.No)))
            {
                this.Cursor = Cursors.WaitCursor;
                base.FormLoaded = false;
                ServiceManager.Instance.ModuleService.MoveTo(base.UserInfo, dragNode.GetValue(colParentID).ToString(), node.GetValue(colParentID).ToString());
                this.FormOnLoad();
                base.FormLoaded = true;
                this.Cursor = Cursors.Default;
            }
        }


        private void emTreeList_DragEnter(object sender, DragEventArgs e)
        {

            dragNode = tvModule.FocusedNode;
            orgParentNode = dragNode.ParentNode;
        }






    }
}